package com.example.zrq1010110087.weight_calculate;

import android.content.Intent;
import android.support.annotation.IdRes;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;

import java.util.regex.Pattern;
/**
 * This is a User operation Interface
 * by inputing height ,you will can obtain a   result about weight in ResultPage  interface when you click on the calculate_button
 * */
public class User_operation extends AppCompatActivity {


    private Button calculate_bt;
    private EditText getheight_et;
    private RadioGroup sexrgroup;
   private PersonUntils person;
    private  EditText getname_et;
    private RadioButton man_radiobt;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.user_operation);
        calculate_bt= (Button) findViewById(R.id.calculate_bt);
        getheight_et=(EditText)findViewById(R.id.getheight_et);
        sexrgroup=(RadioGroup)findViewById(R.id.sex_radiogroup);
       getname_et=(EditText)findViewById(R.id.getname_et);
        man_radiobt=(RadioButton)findViewById(R.id.man_rbt);
        man_radiobt.setChecked(true);
        calculate_bt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                //1.1 从EditText文本中获取height数据和name数据
                //文本name任意,存在匿名情况
                String name=getname_et.getText().toString().trim();
                String  s=getheight_et.getText().toString().trim();
                //1.2 正则表达式判断是否为正整数
                Boolean result=Pattern.compile("^[0-9]*[1-9][0-9]*$").matcher(s).matches();
                /*1.3.1若为正整数,则进一步判断
                * 1.3.2否则提示错误,并且退出*/

                if(result)
                {
                    //限制数据中心所统计的最高标准身高
                    if(Integer.parseInt(s)<40&&Integer.parseInt(s)>250){
                        Toast.makeText(User_operation.this,"非人类无法计算",Toast.LENGTH_SHORT).show();
                        return;

                    }
                    if(person==null)
                    person=new PersonUntils();
                    person.setHeight(Integer.parseInt(s));
                    person.setName(name);

                }else {
                    Toast.makeText(User_operation.this,"你输入的身高有误,请重新输入！",Toast.LENGTH_SHORT).show();
                    return;

                }


                //1.4 检测性别

                switch ( sexrgroup.getCheckedRadioButtonId()){
                           case  R.id.man_rbt: person.setSex("男性");
                                   break;
                           case R.id.female_rbt: person.setSex("女性");
                                   break;
                     }
                //1.5创建intent对象,这是一个启动ResultPageActivity的显示意图
                Intent intent=new Intent(User_operation.this,ResultPage.class);
                //1.6.2将person对象封装在intent对象,借此将数据传递给目标
                intent.putExtra("person",person);
                //1.7启动目标Activity
                startActivity(intent);
            }
        });
    }
}
